package chapter15.fibonacci;

import java.util.Iterator;

/**
 * @author : DuJiabao
 * @Date : 2021/3/4 14:37
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class IterableFabonacci extends Fabonacci implements Iterable<Integer> {
  private int maxSize;  // 边界值

  public IterableFabonacci(int maxSize) {
    this.maxSize = maxSize;
  }

  @Override
  public Iterator<Integer> iterator() {
    return new Iterator<Integer>() {
      @Override
      public boolean hasNext() {
        return maxSize > 0;
      }

      @Override
      public Integer next() {
        maxSize--;
        return IterableFabonacci.this.next(); // 持续返回下一个值
      }
    };
  }

  public static void main(String[] args) {
    Iterator<Integer> iterator = new IterableFabonacci(10).iterator();
    while (iterator.hasNext()) {
      System.out.println(iterator.next());
    }
  }
}
